Skip to content

Cover standard headers behind external headers in the canonical prelude - #304

Open
DrSergei wants to merge 1 commit into
masterfrom
fix/canonical-prelude-external-headers
Open

DrSergei wants to merge 1 commit into
masterfrom
fix/canonical-prelude-external-headers

Conversation

@DrSergei

@DrSergei DrSergei commented Dec 3, 2026

Copy link
Copy Markdown
Member

The canonical preamble scan read only project sources and headers. A standard header reached through an external SDK header (for example <windows.h> through a vendored <uv.h>) stayed uncovered; because canonical indexing keeps separate_system_headers off, its whole compiler-root closure was inlined into every project header and translation unit that included the SDK header, greatly enlarging their expansion text and line maps.

The scan now follows resolved includes outside the compiler-default roots and quoted spellings that resolve inside them from project or external headers, using the run's own project, explicit system, inferred and compiler search paths. It admits the compiler-default angle spellings they reach while the quoted spelling itself stays inline. A translation unit's own quoted compiler-root fallback is not shared: it is expanded once for that unit, and a host fallback such as "p2p.h" would otherwise globalize unrelated platform headers. The C++ standard-library tree is not raw-walked, # include with a separating space is recognized, and spellings discovered only through a followed include are appended after the project's own angle spellings so the baseline include order and macro state are preserved.

Each spelling's search-list result is memoized, as in the preprocessor. On ability_ability_runtime the scan falls from 13.8 s to 1.8-2.1 s, and the canonical run returns to master's wall time (145.6-150.3 s against 145.9-151.8 s) with the same peak (about 1.97 GB). The canonical eval corpora (HDF, HiView, Camera, Ability Runtime) keep their existing counts, and the workspace tests pass.

The canonical preamble scan read only project sources and headers. A
standard header reached through an external SDK header (for example
<windows.h> through a vendored <uv.h>) stayed uncovered; because canonical
indexing keeps separate_system_headers off, its whole compiler-root
closure was inlined into every project header and translation unit that
included the SDK header, greatly enlarging their expansion text and line
maps.

The scan now follows resolved includes outside the compiler-default roots
and quoted spellings that resolve inside them from project or external
headers, using the run's own project, explicit system, inferred and
compiler search paths. It admits the compiler-default angle spellings they
reach while the quoted spelling itself stays inline. A translation unit's
own quoted compiler-root fallback is not shared: it is expanded once for
that unit, and a host fallback such as "p2p.h" would otherwise globalize
unrelated platform headers. The C++ standard-library tree is not
raw-walked, `# include` with a separating space is recognized, and
spellings discovered only through a followed include are appended after
the project's own angle spellings so the baseline include order and macro
state are preserved.

Each spelling's search-list result is memoized, as in the preprocessor.
On ability_ability_runtime the scan falls from 13.8 s to 1.8-2.1 s, and the
canonical run returns to master's wall time (145.6-150.3 s against
145.9-151.8 s) with the same peak (about 1.97 GB). The canonical eval
corpora (HDF, HiView, Camera, Ability Runtime) keep their existing counts,
and the workspace tests pass.

@ArcsinX ArcsinX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and CI status (all checks pass on 61add2b416ba3afe05ae986fbe14c83490f78967). Found one bug in is_cxx_library_path and one minor path-normalization consistency issue in canonical_system_preamble.

.is_some_and(|name| name.starts_with("c++"))
})
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_cxx_library_path checks every component of the full resolved path for name.starts_with("c++") without checking whether resolved is inside roots (or stripping the root prefix).

  1. Any external SDK or project directory whose path happens to contain a component starting with c++ (e.g., /workspace/c++-sdk/include/api.h, third_party/c++20/...) is misclassified as a C++ standard-library tree and skipped, so standard headers reached through it are never added to the canonical preamble.
  2. Even for headers inside roots, if a compiler root itself lives under an ancestor directory starting with c++ (e.g., /opt/c++-15/include/platform.h), is_cxx_library_path matches that ancestor and skips following quoted compiler headers under that root.

Consider restricting this check to roots and inspecting only the components relative to the matching root (e.g. roots.iter().find_map(|root| path.strip_prefix(root).ok())).

search: &PreprocessOptions,
files: &[PathBuf],
) -> Option<(PathBuf, tempfile::NamedTempFile)> {
let translation_units: HashSet<PathBuf> = files.iter().cloned().collect();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial_sources below normalizes entries with graph.intern_path(file), whereas translation_units clones files directly. Using files.iter().map(|file| graph.intern_path(file)).collect() would ensure translation_units.contains(&file) still matches if a caller passes a non-canonical path in files.

@devnullexile devnullexile left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 16 canonical-system tests pass. Separate fixtures showed that the scan can seed macros from the wrong header, change C call edges, and consume a cached external input without reporting it under --require-local-inputs.

// angle spelling; the prelude run then follows its nested
// includes. Reading it here as well would repeat that whole
// closure.
if admissible {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Resolve transitive angle includes before admitting them. If an external wrapper.h includes <config.h> and both -I sdk and a compiler root supply it, this early exit adds the compiler copy to the prelude and skips sdk/config.h. I reproduced an unrelated TU taking a branch enabled by the unused compiler copy, while the standard header included by the SDK copy stayed inline.

.chain(&search.after_include_paths);
let mut found = search_dirs
.find(|dir| scan_include_exists_in(search, dir, spelling))
.map(|dir| trace_ir::canonicalize(&dir.join(spelling)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve the path used to reach a symlinked header. If sdk/api.h points to ../origin/api.h and includes "detail.h", preprocessing searches sdk/, but this canonicalized result makes the scan search origin/. With a detail.h in each directory, the scan seeded every TU with a macro from the wrong one. Keep the includer’s lookup directory when queuing and deduplicating scan work.

continue;
}
let deferred_file = !initial.contains(&file);
let text = match graph.source_cache.get(&file) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Read followed SDK headers from the effective search.source_cache. When a caller supplies replacement text for an on-disk sdk/wrapper.h, preprocessing reads the replacement while this scan reads the disk copy. A standard include present only on disk then seeds macros into unrelated TUs. A virtual-only wrapper is skipped entirely because the disk read fails.

if is_cxx_library_path(&resolved) {
continue;
}
if roots.iter().any(|root| resolved.starts_with(root))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep rejected compiler-root angle includes out of the C scan. In a project header with #ifdef __cplusplus around <vector>, C rejects the extensionless spelling but still queues the file when the compiler root has no c++ component. With vector including a private .h, I reproduced that header’s macro changing a call edge in an unrelated C file. Limit traversal into compiler roots here to the intended quoted-header case.

// The scan reads this file only as metadata; still report it
// as an input under `--require-local-inputs`.
if let Some(boundary) = &search.input_boundary {
boundary.record(&file);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Record the input boundary on graph-cache hits too. An external --dep header is already in graph.source_cache. I reached one only via # include "dep.h" inside #if 0, so ordinary preprocessing never opened it. Its contents still selected a header from an in-tree compiler root and changed the call graph, while the --require-local-inputs collector reported no external path.

component
.as_os_str()
.to_str()
.is_some_and(|name| name.starts_with("c++"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Restrict the standard-library exclusion to compiler-owned paths. An ordinary SDK installed under /opt/c++sdk/include matches this prefix, so the scan skips its wrapper and leaves the standard headers behind it inline for every consumer. I verified that renaming the SDK’s parent directory alone changes whether its nested standard header enters the prelude.

) -> Option<PathBuf> {
let spelling_path = Path::new(spelling);
if spelling_path.is_absolute() {
return scan_include_exists(search, spelling_path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Preserve the non-strict basename fallback for a missing absolute include. Given #include "/stale/build/include/wrapper.h" and one wrapper.h in a dependency root, preprocessing recovers that header and expands its nested standard include. This early return ends the scan at the missing path, so that standard header stays out of the prelude.

Comment thread docs/PREPROCESSOR.md
the language's first compiler-default root.
it inline once. Those units and headers are raw-scanned transitively
through their includes outside the compiler-default roots and through
quoted spellings that resolve inside them, so standard headers reached

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Document the exception for quoted includes written directly in a translation unit. For main.cpp including "platform.h" from a compiler root, the scanner deliberately skips that header even if it includes <nested.h>. This paragraph currently promises traversal for both units and headers, which gives the wrong expectation about whether nested.h is shared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants